layui.use(['table', 'form', 'jquery','notice'], function() {
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    let notice = layui.notice;

    let cols = [
        [{
            type: 'checkbox'
        },
            {
                title: '编号',
                field: 'id',
                align: 'center',
                width: 80
            },
            {
                title: '标签名字',
                field: 'tag_name',
                align: 'center',
            },
            {
                title: '启用',
                field: 'status',
                align: 'center',
                width: 100,
                templet: '#tags-enable'
            },
            {
                title: '创建时间',
                field: 'create_time',
                align: 'center',
                width: 160,
                templet: '#tags-createTime'
            },
            {
                title: '操作',
                toolbar: '#tags-bar',
                align: 'center',
                width: 130
            }
        ]
    ]

    table.render({
        elem: '#tags-table',
        url: '/admin.php/tags/index',
        page: true,
        cols: cols,
        skin: 'line',
        toolbar: '#tags-toolbar',
        response:{
            statusName:'code',
            statusCode:1,
        },
        defaultToolbar: [{
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports']
    });

    table.on('tool(tags-table)', function(obj) {
        if (obj.event === 'remove') {
            window.remove(obj);
        } else if (obj.event === 'edit') {
            window.edit(obj);
        }
    });

    window.remove = function(obj) {
        layer.confirm('确定要删除该数据', {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            $.ajax({
                url:'/admin.php/tags/del',
                data:{id:obj.data['id']},
                type: 'POST',
                success: function(result) {
                    if (result.code) {
                        notice.success(result.msg)
                        obj.del();
                    } else {
                        notice.error(result.msg)
                    }
                }
            })
        });
    }

    table.on('toolbar(tags-table)', function(obj) {
        if (obj.event === 'add') {
            window.add();
        } else if (obj.event === 'refresh') {
            window.refresh();
        } else if (obj.event === 'batchRemove') {
            window.batchRemove(obj);
        }
    });

    form.on('submit(user-query)', function(data) {
        table.reload('user-table', {
            where: data.field
        })
        return false;
    });

    form.on('switch(tags-enable)', function(data) {
        var status = data.elem.checked?1:0;
        var id = this.value;
        $.post('/admin.php/tags/status',{status:status,id:id},function (result) {
            if (result.code){
                notice.success(result.msg)
            } else {
                notice.error(result.msg)
            }
        })
    });

    if (typeof width !== 'number' || width === 0) {
        width = $(window).width();
    }
    if (typeof height !== 'number' || height === 0) {
        height = $(window).height();
    }

    window.add = function() {
        layer.open({
            type: 2,
            scrollbar: true,
            title: '新增',
            shade: false,
            resize:true,
            maxmin:true,
            isOutAnim:false,
            anim:2,
            area: [width + 'px', height + 'px'],
            content:  '/admin.php/tags/add',
            end:function () {
                table.reload('tags-table');
            }
        });
    }

    window.edit = function(obj) {
        layer.open({
            type: 2,
            scrollbar: true,
            title: '修改',
            shade: false,
            resize:true,
            maxmin:true,
            isOutAnim:false,
            anim:2,
            area: [width + 'px', height + 'px'],
            content: '/admin.php/article/edit'
        });
    }

    window.batchRemove = function(obj) {
        let data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            notice.warning("未选数据");
            return false;
        }
        let ids = "";
        for (let i = 0; i < data.length; i++) {
            ids += data[i].id + ",";
        }
        ids = ids.substr(0, ids.length - 1);
        layer.confirm('确定删除这些标签', {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            $.ajax({
                url:  "/admin.php/tags/delAll",
                data:{ids:ids},
                type: 'post',
                success: function(result) {
                    if (result.code) {
                        notice.success(result.msg)
                        table.reload('tags-table');
                    } else {
                        notice.error(result.msg)
                    }
                }
            })
        });
    }

    window.refresh = function(param) {
        table.reload('tags-table');
    }
})